from django.db import models
from django.utils import timezone
from apps.users.models import User
from apps.boards.models import ParentBoard, Board


class Article(models.Model):
    """文章模型"""
    
    article_id = models.AutoField(primary_key=True, verbose_name='Article ID')
    p_board = models.ForeignKey(ParentBoard, on_delete=models.CASCADE, verbose_name='Parent Board')
    board = models.ForeignKey(Board, on_delete=models.CASCADE, blank=True, null=True, verbose_name='Sub Board')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='Publisher')
    title = models.CharField(max_length=200, verbose_name='Article Title')
    content = models.TextField(verbose_name='Article Content')
    cover = models.CharField(max_length=500, blank=True, null=True, verbose_name='Cover Image')
    top_type = models.BooleanField(default=False, verbose_name='Is Notice')
    top_expire_time = models.DateTimeField(blank=True, null=True, verbose_name='Top Expire Time')
    expire_time = models.DateTimeField(blank=True, null=True, verbose_name='Expire Time')
    allow_comments = models.BooleanField(default=True, verbose_name='Allow Comments')
    good_count = models.IntegerField(default=0, verbose_name='Like Count')
    comment_count = models.IntegerField(default=0, verbose_name='Comment Count')
    post_time = models.DateTimeField(default=timezone.now, verbose_name='Post Time')
    update_time = models.DateTimeField(auto_now=True, verbose_name='Last Update Time')
    
    class Meta:
        db_table = 'articles'
        verbose_name = 'Article'
        verbose_name_plural = 'Articles'
        indexes = [
            models.Index(fields=['p_board']),
            models.Index(fields=['board']),
            models.Index(fields=['user']),
            models.Index(fields=['post_time']),
            models.Index(fields=['top_type']),
            models.Index(fields=['expire_time']),
        ]
        
    def __str__(self):
        return self.title
    
    @property
    def p_board_name(self):
        """动态获取父板块名称"""
        return self.p_board.board_name if self.p_board else None
    
    @property
    def board_name(self):
        """动态获取子板块名称"""
        return self.board.board_name if self.board else None
    
    @property
    def nick_name(self):
        """动态获取用户昵称"""
        return self.user.nickname if self.user else None
        
    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)


class ArticleImage(models.Model):
    """文章图片模型"""
    image_id = models.AutoField(primary_key=True, verbose_name='Image ID')
    article = models.ForeignKey(
        Article, 
        on_delete=models.CASCADE, 
        related_name='images',
        verbose_name='Article'
    )
    image_path = models.CharField(max_length=500, verbose_name='Image Path')
    create_time = models.DateTimeField(default=timezone.now, verbose_name='Create Time')
    
    class Meta:
        db_table = 'article_images'
        verbose_name = 'Article Image'
        verbose_name_plural = 'Article Images'
        indexes = [
            models.Index(fields=['article']),
        ]
        
    def __str__(self):
        return f"{self.article.title} - Image{self.image_id}"


class Like(models.Model):
    """点赞模型"""
    LIKE_TYPE_CHOICES = [
        (0, 'Article'),
        (1, 'Comment'),
    ]
    
    like_id = models.AutoField(primary_key=True, verbose_name='Like ID')
    like_type = models.IntegerField(choices=LIKE_TYPE_CHOICES, verbose_name='Like Type')
    object_id = models.IntegerField(verbose_name='Object ID (Article ID or Comment ID)')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='Like User')
    author_user_id = models.CharField(max_length=15, blank=True, null=True, verbose_name='Main Object Author ID')
    create_time = models.DateTimeField(default=timezone.now, verbose_name='Create Time')
    
    class Meta:
        db_table = 'likes'
        verbose_name = 'Like'
        verbose_name_plural = 'Likes'
        unique_together = ['object_id', 'user', 'like_type']
        indexes = [
            models.Index(fields=['user']),
            models.Index(fields=['like_type']),
        ]
        
    def __str__(self):
        return f"{self.user.nickname} - {self.get_like_type_display()}"